import json
import pymysql.cursors
import re


class Mysql:

    def __init__(self, config):
        config["cursorclass"] = pymysql.cursors.DictCursor
        self.connection = pymysql.connect(**config)

    def query_all(self, sql):
        with self.connection.cursor() as cursor:
            cursor.execute(sql)
            datas = cursor.fetchall()
            return datas

    def query_one(self, sql):
        with self.connection.cursor() as cursor:
            cursor.execute(sql)
            data = cursor.fetchone()
        return data

    def update(self, sql):
        with self.connection.cursor() as cursor:
            cursor.execute(sql)
            self.connection.commit()
            print("update ok .")

    def delete(self, sql):
        with self.connection.cursor() as cursor:
            cursor.execute(sql)
            self.connection.commit()
            print("delete ok .")

    def insert(self, sql, list_data):
        with self.connection.cursor() as cursor:
            cursor.executemany(sql, list_data)
            self.connection.commit()
            print("insert ok .")

    def insert_one(self, sql, list_data):
        with self.connection.cursor() as cursor:
            cursor.execute(sql, list_data)
            self.connection.commit()
            print("insert ok .")

    def close(self):
        self.connection.close()


if __name__ == "__main__":
    pass